﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using PornSite.Web;
using System.ServiceModel.DomainServices.Client;

namespace PornSite.Views
{
    public partial class ManagerPhoto : Page
    {
        PornSite.Web.DomainServicePorn context = new DomainServicePorn();
        public ManagerPhoto()
        {
            InitializeComponent();
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

            if (WebContext.Current.Authentication.User.Identity.IsAuthenticated) 
            {
                this.LayoutRoot.Visibility = System.Windows.Visibility.Visible;
            }
        }

        private void aspnet_UsersDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void ButtonSaveFolder_Click(object sender, RoutedEventArgs e)
        {
            this.aspnet_UsersDomainDataSource.SubmitChanges();
        }

        private void aspnet_UsersListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selected = (aspnet_Users)this.aspnet_UsersListBox.SelectedItem;
            if (selected != null && selected.LoweredUserName == WebContext.Current.Authentication.User.Identity.Name)
            {
                this.editFolders.Visibility = System.Windows.Visibility.Visible;
            }
            else 
            {
                this.editFolders.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void foldersDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (foldersDataGrid.SelectedItem != null)
            {
                var selected = (Folder)foldersDataGrid.SelectedItem;
                int id = selected.FolderId;
                context.Load(context.GetPublicPhotosQuery(id)).Completed += (s, a) =>
                {
                    LoadOperation<Photo> photos = (LoadOperation<Photo>)s;
                    this.photoRadGridView.ItemsSource = photos;
                };
            }
        }

    }
}
